package com.icat.house.service.market;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class SmsService {
	private static transient Prop prop = PropKit.use("oauth.properties");

	/**
	 * 发送验证码
	 * 
	 * @param mobile
	 * @param authCode
	 * @return
	 */
	public static final boolean sendAuthCode(String mobile, int authCode) {
		boolean flag = false;
		String url = prop.get("url");
		String appkey = prop.get("appkey");
		String secret = prop.get("secret");
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName("互动51");
		req.setSmsParamString("{\"code\":\"" + authCode + "\",\"phone\":\"" + mobile + "\"}");
		req.setRecNum(mobile);
		req.setSmsTemplateCode("SMS_47565135");
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
			String body = rsp.getBody();
			JSONObject jsonObject = new JSONObject();
			jsonObject = JSON.parseObject(body);
			if (jsonObject.containsKey("alibaba_aliqin_fc_sms_num_send_response")) {
				if (jsonObject.getJSONObject("alibaba_aliqin_fc_sms_num_send_response").containsKey("result")) {
					flag = JSON.parseObject(body).getJSONObject("alibaba_aliqin_fc_sms_num_send_response")
							.getJSONObject("result").getBoolean("success");
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 发送用户短信息
	 * 
	 * @param phone
	 * @param name
	 * @param money
	 * @return
	 */
	public static final boolean sendInfo(String phone, String name, String time) {
		boolean flag = false;
		String url = prop.get("url");
		String appkey = prop.get("appkey");
		String secret = prop.get("secret");
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName("互动51");
		req.setSmsParamString("{\"n\":\"" + name + "\",\"t\":\"" + time + "\",\"p\":\"" + "010-20321233" + "\"}");
		req.setRecNum(phone);
		req.setSmsTemplateCode("SMS_109480120");
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
			String body = rsp.getBody();
			JSONObject jsonObject = new JSONObject();
			jsonObject = JSON.parseObject(body);
			if (jsonObject.containsKey("alibaba_aliqin_fc_sms_num_send_response")) {
				if (jsonObject.getJSONObject("alibaba_aliqin_fc_sms_num_send_response").containsKey("result")) {
					flag = JSON.parseObject(body).getJSONObject("alibaba_aliqin_fc_sms_num_send_response")
							.getJSONObject("result").getBoolean("success");
				}
			}
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return flag;
	}

}
